<template>
    <div class="app-container">
      <el-row :gutter="20">
        <el-col :span="24" :xs="24">
          <el-form
            v-show="showSearch"
            ref="queryForm"
            :model="queryParams"
            :inline="true"
            label-width="68px"
          >
          <el-form-item label="姓名" prop="orderNumber">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入姓名"
                clearable
                size="small"
                style="width: 180px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="请假类型" prop="leaveType">
                <el-select
                    v-model="queryParams.leaveType"
                    placeholder="选择状态"
                    clearable
                    size="small"
                    style="width: 180px"
                >
                    <el-option :key="0" label="事假" :value="0" />
                    <el-option :key="1" label="病假" :value="1" />
                </el-select>
            </el-form-item>
            <el-form-item label="请假状态" prop="state">
                <el-select
                    v-model="queryParams.state"
                    placeholder="选择状态"
                    clearable
                    size="small"
                    style="width: 180px"
                >
                    <el-option :key="0" label="等待批准" :value="0" />
                    <el-option :key="1" label="请假已取消" :value="1" />
                </el-select>
              <el-button
                  style="margin-left: 50px;"
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="getLeave"
                >搜索</el-button>
            </el-form-item>
          </el-form>
          <el-table :data="leaveList" style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column prop="courierName" label="请假人" align="center" />
            <el-table-column prop="leaveType" label="请假类型" align="center">
                <template #default="scope">
                    <strong :class="[scope.row.leaveType == 0 ? 'select1' : 'select2']">{{ scope.row.state == 0 ? "病假" : "事假" }}</strong>
                </template>
            </el-table-column>
            <el-table-column prop="startTime" label="请假开始时间" align="center" />
            <el-table-column prop="endTime" label="请假结束时间" align="center" />
            <el-table-column prop="state" label="申请状态" align="center">
                <template #default="scope">
                    <strong :class="[scope.row.state == 0 ? 'select1' : 'select2']">{{ scope.row.state == 0 ? "等待批准" : "请假已取消" }}</strong>
                </template>
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              width="160"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button v-if="scope.row.state == 0"
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                >去批准</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-pagination
        :page-size.sync="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-sizes="[10, 20, 30, 40]"
        :current-page.sync="queryParams.pageNum"
        @current-change="getLeave"
        @size-change="getLeave"
      />
      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body @close="onClose">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="14">
              <el-form-item label="请假人" prop="courierName">
                <el-input
                  v-model="form.courierName"
                  maxlength="11"
                  :disabled="true"
                />
              </el-form-item>
            </el-col>
            <el-col :span="10">
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="开始时间" prop="startTime">
                <el-input
                  v-model="form.startTime"
                  maxlength="100"
                  :disabled="true"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="请假类型" prop="leaveType">
                <el-select
                    v-model="form.leaveType"
                    placeholder="选择状态"
                    clearable
                    style="width: 240px"
                    :disabled="true"
                >
                    <el-option :key="0" label="事假" :value="0" />
                    <el-option :key="1" label="病假" :value="1" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="结束时间" prop="endTime">
                <el-input
                  v-model="form.endTime"
                  maxlength="100"
                  :disabled="true"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="请假状态" prop="leaveType">
                <el-select
                    v-model="form.leaveType"
                    placeholder="选择状态"
                    clearable
                    :disabled="true"
                    style="width: 240px"
                >
                    <el-option :key="0" label="等待批准" :value="0" />
                    <el-option :key="1" label="请假已取消" :value="1" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="请假原因" prop="address">
                <el-input
                  v-model="form.incident"
                  maxlength="30"
                  :disabled="true"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="" prop="courierName">
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否同意" prop="ratifyType">
                <el-select
                    v-model="form.ratifyType"
                    placeholder="选择状态"
                    style="width: 240px"
                >
                    <el-option :key="0" label="同意请假" :value="0" />
                    <el-option :key="1" label="拒绝请假" :value="1" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  // import { getToken } from '@/utils/auth'
  
  import { listLeave,ratifyLeave,delLeave } from '@/api/leave/index.js'
  import {
    listStaff
  }
  from '@/api/staff/staff.js'
  import { 
    queryStudyAll 
  } from "@/api/content/tag";
import { color } from 'echarts';
  export default {
    name: 'Parcel',
    data() {
      return {
        exportLoading:false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          isRatify: 0,
          leaveType: undefined,
          state: undefined,
          name:undefined
        },
        title: '',
        // 是否显示弹出层
        open: false,
        // 表单校验
        rules: {
          // userName: [
          //   { required: true, message: '用户名称不能为空', trigger: 'blur' },
          //   {
          //     min: 2,
          //     max: 20,
          //     message: '用户名称长度必须介于 2 和 20 之间',
          //     trigger: 'blur'
          //   }
          // ],
          headNumber: [
            { required: true, message: '学号只能为8位数', trigger: 'blur',max:8,min:8 }
          ],
          password: [
            { min: 1,
              max: 1,
              required: true, 
              message: '用户密码必须有英文加数字组成', 
              trigger: 'blur' 
            }
          ],
        },
        // 角色选项
        roleOptions: [],
        fileList:[],
        // 显示搜索条件
        showSearch: true,
        staffList:[],
        // 遮罩层
        loading: true,
        // 用户表格数据
        floristList: null,
        leaveList:[],
        // 总条数
        total: 0,
        // 选中数组
        ids: [],
        // 表单参数
        form:{
            id:undefined,
            courierId:undefined,
            courierName:undefined,
            leaveType:undefined,
            state:undefined,
            startTime:undefined,
            endTime:undefined,
            incident:undefined,
            isRatify:undefined,
            ratifyType: undefined
        }
      }
    },
    watch: {},
    created() {
      this.getLeave()
      this.getStaff()
    },
    methods: {
     onClose() {
        this.reset()
     },
     getStaff() {
        listStaff(this.queryParams).then((response) => {
            this.staffList = response.rows
        })
      },
     anewOrderNumber(){
        this.form.orderNumber = Math.floor(Math.random() * 900000000000) + 1
     },
    //  生成一个四位数的随机数字
     anewFetchGoodsNumber(){
        var number = Math.floor(Math.random() * 10000)
        while(number.toString().length < 4){
            number = Math.floor(Math.random() * 10000)
        }
        this.form.fetchGoodsNumber = String.fromCharCode(Math.floor(Math.random() * 26) + 65)+'-'+ (Math.floor(Math.random() * 9)+1) +'-'+number
     },
     handleInput(value) {
      const reg = /^[\d\.]+$/;
      if (value && !reg.test(value)) {
        this.form.age = value.replace(/[^\d\.]/g, '');
        this.form.phone = value.replace(/[^\d\.]/g, '');
      }
     },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getLeave()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      getLeave() {
        listLeave(this.queryParams).then((response) => {
            this.leaveList = response.rows
            this.total = response.total
        })
      },
      // 表单重置
      reset() {
        this.form = {
          phone: undefined,
          id: undefined,
          orderNumber: undefined,
          fetchGoodsNumber: undefined,
          fetchGoodsPerson: undefined,
          courierId: undefined,
          courierName: undefined,
          address: undefined,
          state: 0
        }
        this.resetForm('form')
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.open = true
        this.form.id = row.id
        this.form.courierId = row.courierId
        this.form.courierName = row.courierName
        this.form.leaveType = row.leaveType
        this.form.startTime = row.startTime
        this.form.endTime = row.endTime
        this.form.incident = row.incident
        this.form.isRatify = row.isRatify
        this.form.ratifyType = row.ratifyType
        this.form.state = row.state
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$modal
          .confirm('是否确认删除编号为"' + row.orderNumber + '"的数据项？')
          .then(function() {
            return delLeave(ids)
          })
          .then(() => {
            this.getLeave()
            this.$modal.msgSuccess('删除成功')
          })
          .catch(() => {})
      },
      /** 提交按钮 */
      submitForm: function() {
        this.form.isRatify = 1
        this.$refs['form'].validate((valid) => {
          if (valid) {
            if (this.form.id !== undefined) {
              ratifyLeave(this.form).then((response) => {
                this.$modal.msgSuccess('批准成功')
                this.open = false
                this.getLeave()
                this.reset()
              })
            } else {
              //添加走这段逻辑
              addExpressage(this.form).then((response) => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.reset()
                this.getLeave()
              })
            }
          }
        })
      }
    }
  }
  </script>
  <style scoped>
  div .upload-demo {
    /* padding-left: 80px; */
  }
  .el-col .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
  
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

  .select2 {
    color: red;
    
  }
  .select1 {
    color: green;
  }
  </style>
  